﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Configuration;

using ByHand.Data.Interfaces;

namespace ByHand.Data
{
    public class DatabaseFactory : Disposable, IDatabaseFactory
    {
        private ByHandDB db;
        public ByHandDB Get()
        {
            return db ?? (db = new ByHandDB(ConfigurationManager.ConnectionStrings["ByHandDB"].ConnectionString));
        }

        protected override void DisposeCore()
        {
            if (db != null)
                db.Dispose();
        }
    }

    public class Disposable : IDisposable
    {
        private bool isDisposed;

        ~Disposable()
        {
            Dispose(false);
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        private void Dispose(bool disposing)
        {
            if (!isDisposed && disposing)
            {
                DisposeCore();
            }

            isDisposed = true;
        }
        protected virtual void DisposeCore()
        {
        }
    }
}
